package org.wattdepot.cli;

import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.util.tstamp.Tstamp;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.List;

/**
 * Generates a value based on the location of the plant and the time stamp.
 * @author wing
 */
public class PowerGeneratedTimeStampCommand extends CommandLineInterface 
implements WattDepotCliCommand {

  /**
   * Takes two values source and timestamp to find the power generated.
   * @param args list of parameters for how to handle all the power data.
   * @return a string that contains the power generated with applied statistics parameters.
   */
  @Override
  public String doCommand(List<String> args) {
    
    String type = args.get(0);
    String source = args.get(1);
    String timeStamp = args.get(2);
    String consumedOption = "consumed";
    String generatedOption = "generated";
    Source serr = null;
    try {
      serr = searchSource(source);
    }
    catch (WattDepotClientException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }
    
    if (serr == null) {
      return sourceNotFound;
    }
   
    
    XMLGregorianCalendar now = null;
    try {
      now = Tstamp.makeTimestamp(timeStamp);
    }
    catch (Exception e) {
      System.out.println("should never happen.");
    }
    
    
    String powerGen = "";
    try {
    //both options require the same inputs
    if (type.equalsIgnoreCase(generatedOption)) { 
      powerGen = Double.toString(client.getPowerGenerated(source, now));
    }
    
    else if (type.equalsIgnoreCase(consumedOption)) {
      powerGen = Double.toString(client.getPowerConsumed(source, now));      
    }
    
    }
    catch (NotAuthorizedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (ResourceNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (BadXmlException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    catch (MiscClientException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
    return powerGen;
  }
 
}
